<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5" type="home">
{
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '首页',
    // #ifdef MP-ALIPAY
    enablePullDownRefresh: false,
    'mp-alipay': {
      allowsBounceVertical: 'NO',
      transparentTitle: 'always',
      titlePenetrate: 'YES',
      defaultTitle: '',
    },
    // #endif
  },
}
</route>
<template>
  <CustomNavbar />
  <scroll-view
    :scroll-top="scrollTop"
    :scroll-y="true"
    class="scroll-Y"
    refresher-enabled
    :refresher-triggered="isTriggered"
    @refresherrefresh="upper"
    @scrolltolower="lower"
  >
    <uni-notice-bar
      style="font-size: 3rpx"
      show-icon
      scrollable
      :text="noticeContent"
      :speed="70"
      color="#ac93ca"
      :open="true"
    />
    <!-- <Types /> -->
    <Reports ref="reportsRef" />
    <Function />
    <MySwiper />
    <Guess ref="guessRef" />
    <div style="width: 100rpx; height: 300rpx"></div>
  </scroll-view>
</template>

<script lang="ts" setup>
import MySwiper from '@/components/MySwiper.vue'
import CustomNavbar from './components/CustomNavbar.vue'
import Types from './components/Types.vue'
import { GuessInstance, ReportsInstance } from '@/types/components'
import Guess from '@/components/Guess.vue'
import Function from './components/Function.vue'
import Reports from './components/Reports.vue'
import { loginApi } from '@/service/user'
import { useUserStore } from '@/store/user'
import { useBookStore } from '@/store/book'
import { myBookListApi } from '@/service/book'
import { getNoticeContentApi } from '@/service/global'
import { Scene } from '@/types/global'

const scrollTop = ref(0)
const userInfoRef = useUserStore()
const bookRef = useBookStore()
const noticeContent = ref<string>('')
const guessRef = ref<GuessInstance>()
const reportsRef = ref<ReportsInstance>()

onLoad(async (req: any) => {
  // 获取用户信息
  uni.showLoading({ title: '加载中...', mask: true })
  const loginRes = await wx.login()
  const loginVo = await loginApi(1, loginRes.code)

  userInfoRef.setUserInfo({
    id: loginVo.data.id,
    token: loginVo.data.token,
    avatar: loginVo.data.avatar,
    nickname: loginVo.data.nickname,
    birthday: loginVo.data.birthday,
    city: loginVo.data.city,
    sex: loginVo.data.sex,
    phone: loginVo.data.phone,
  })

  reportsRef.value?.resetData()
  const bookListRes = await myBookListApi({ page: 1, size: 10 })
  bookRef.bookList = bookListRes.data.records
  const noticeRes = await getNoticeContentApi()
  noticeContent.value = noticeRes.data

  uni.hideLoading()
})

// 触发下拉刷新
const upper = async () => {
  console.log('下拉刷新')
  isTriggered.value = true
  // 加载数据 api 要加 await
  // ...
  await Promise.all([guessRef.value?.resetData(), reportsRef.value?.resetData()])
  setInterval(() => {
    isTriggered.value = false
  }, 1000)
}
const isTriggered = ref(false)

const lower = () => {
  console.log('lower')
  guessRef.value?.getMore()
}
</script>

<style>
page {
  background: #f6f6f6;
}

.scroll-Y {
  width: 100%;
  height: calc(100vh - var(--window-top));
  /* min-height: 100vh; */
}
</style>
